<?php

class CC_View_Helper_FlagPrivilege extends Zend_View_Helper_Abstract
{
    public function flagPrivilege($sourceElement = '#flags', $targetElement = '#privilege')
    {
        $jquery = $this->view->jQuery();
        $jquery->enable();
        
        $jqHandler = ZendX_JQuery_View_Helper_JQuery::getJQueryHandler();
        
        $function = <<<EOD
            $("{$sourceElement}").change(function(){
            	var selectedValue = $("{$sourceElement} option:selected").val();
            	var selectOptions = '';
            	
            	$("{$targetElement}").html("<option value=''>Loading...</option>");
            	
            	$.post(
            		'/backoffice/async/getprivilegesforflag',
            		{flag: selectedValue},
            		function(data) {
            			$("{$targetElement}").html("");
            			
            			if(data.length > 0)
            			{
                			for(var i = 0; i < data.length; i++)
                			{
                				$("{$targetElement}").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
                			}
            			}
            			else
            			{
            				$("{$targetElement}").append("<option value=''>No privileges</option>");
            			}
            		},
            		'json'
            	);
            });
EOD;
        
        $jquery->addOnload($function);
    }
}

?>